<?php
########################################################################################
# Empresa :     Tecnopac Informatica;                                                  #
# Autor:        Deiverson Pacheco;                                                     #
# Data Criação: 10/12/2010;                                                            #
########################################################################################
session_start();
header("Cache-control: private");
include_once("../../includes/session.php");


//Linha para formatação de caracteres com acentuação
header("Content-Type: text/html; charset=ISO-8859-1",true);


//======================================================================================
//==> Include obrigatórias no script
//======================================================================================
include_once('../../includes/classes/class.TemplatePower.php');
include_once('../../includes/classes/class.Conexao.php');
include_once('../../includes/classes/class.Geral.php');

//======================================================================================
//==> Inicializar Classes
//======================================================================================
$not  = new Persist;
$data = new getData;
$cor  = new ColorTr;
$util = new Util;
$vld  = new ValidarDados;
$sql  = new SqlInjection;

//======================================================================================
//==> Instanciar TemplatePower para juntar o PHP + HTML
//======================================================================================
$tpl  = new TemplatePower('../../html/geral/categorias_man.html');
$tpl->prepare();


//======================================================================================
//==> Recebe variaveis de pesquisa
//======================================================================================
$id          = @$_GET['id'];
$ncategoria  = @$_GET['ncategoria'];
$evento      = @$_GET['evento'];

$tpl->assign('botao2','<input type="button" value="Voltar a consulta" onClick="volta_list_categorias();" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">');

if($evento != 'editar'){
	$tpl->assign('botao','<input type="button" value="Salvar novo" onClick="salvar_categorias();" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">');
}else{
	$tpl->assign('botao','<input type="button" value="Salvar altera&ccedil;&atilde;o" onClick="salvar_edit_categorias();" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only">');
}

//======================================================================================
//==> Executa case conforme evento
//======================================================================================

if(isset($evento)){
	switch($evento){
		
		case 'salvar': {
		  	$ncategoria = $sql->tratar($ncategoria,100);
			
			//captura data e hora atual
			$dt = $data->getDataHoje('-').' '.$data->getHora();
			
			$not->sql = "insert into categorias
						(categoria,uid,ativo,datahora,uid_alteracao,dt_hora_alteracao) 
						 values 
						('$ncategoria','$ID_USUARIO','Sim','$dt','0','0000-00-00')";
			$not->ins();
			
			$not->sql = "SELECT MAX(id) AS id FROM categorias";
			$not->ler();
			$not->ini();
			
			print "php/geral/categorias_man.php?evento=editar&id=$not->id";
			exit;

		break;
		}
		
		
		case 'editar': {
		  	$id = $sql->tratar($id,10);
			
			$not->sql = "select id, categoria from categorias where id = '$id'";
			if($not->ler()){
			   $not->ini();
			   $tpl->assign('id',$not->id);
			   $tpl->assign('categoria',$not->categoria);
			}

		break;
		}
		
		

		case 'salvar_edit': {
		  	$ncategoria = $sql->tratar($ncategoria,100);
			
			//captura data e hora atual
			$dt = $data->getDataHoje('-').' '.$data->getHora();
			
			$not->sql = "Update categorias set categoria = '$ncategoria' where id = '$id'";
			$not->alt();
			
			print "php/geral/categorias_man.php?evento=editar&id=$id";
			exit;

		break;
		}


		case 'excluir': {
			$codigos = explode(";",$id);
			for($i=1;count($codigos)>$i;$i++){ 
				$not->sql = "update categorias set ativo='Nao' where id = '$codigos[$i]'";
				$not->alt();
			}		
				
			print "php/geral/categorias.php";
			exit;
		break;
		}
		
		
		
	}
}


//======================================================================================
//==> Enviar página unificada PHP + HTML
//======================================================================================
$tpl->showUnAssigned( false );  // para debug. na hora de publicar, setar para false
$tpl->printToScreen();
?>